<template>
  <!--引用表格 remainingMember-->
  <a-descriptions :title="props.depname" bordered
    v-if="props.depname == '齐齐哈尔市国有资产监督管理委员会' || props.depname == '领导班子' ? false : true">
    <a-descriptions-item label="企业级别">{{ props.Companydata.memo || '无' }}</a-descriptions-item>
    <a-descriptions-item label="核定员额">{{ props.Companydata.member || '无' }}</a-descriptions-item>
    <a-descriptions-item label="实有员额">{{ props.Companydata.realMember || '无' }}</a-descriptions-item>
    <a-descriptions-item label="剩余员额">{{ props.Companydata.remainingMember || '无' }}</a-descriptions-item>
    <!-- translateNumber(props.Companydata.deputyPosition) -->
    <!-- num == 0 ? '' : -->
    <a-descriptions-item label="核定领导数">{{props.Companydata.position}}</a-descriptions-item>
    <a-descriptions-item label="实配领导数">{{props.Companydata.realPosition}}</a-descriptions-item>
    <a-descriptions-item label="核定内设机构数">{{ props.Companydata.internalInstitutions || '无' }}</a-descriptions-item>
  </a-descriptions>
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <!--插槽:table标题-->
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="selectAddUser"
        v-if="props.showEdit">添加已有用户</a-button>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="createUser"
        v-if="props.showEdit">新建用户</a-button>
      <a-button type="primary" @click="openModal(true, {})" preIcon="ant-design:hdd-outlined"> 回收站</a-button>
      <template v-if="selectedRowKeys.length > 0">
        <a-dropdown>
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="onUnlinkDepartUserBatch" v-if="props.showEdit">
                <icon icon="bx:bx-unlink" />
                <span>取消关联</span>
              </a-menu-item>
            </a-menu>
          </template>
          <a-button v-if="props.showEdit">
            <span>批量操作 </span>
            <icon icon="akar-icons:chevron-down" />
          </a-button>
        </a-dropdown>
      </template>
    </template>
    <!-- 插槽：行内操作按钮v-if="props.showEdit" -->
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
    </template>
  </BasicTable>
  <UserDrawer @register="registerDrawer" @success="onUserDrawerSuccess" />
  <DepartRoleUserAuthDrawer @register="registerUserAuthDrawer" />
  <UserSelectModal rowKey="id" @register="registerSelUserModal" @getSelectResult="onSelectUserOk" />
  <!--回收站-->
      <!--修改密码-->
      <PasswordModal @register="registerPasswordModal" @success="reload" />
  <UserRecycleBinModal @register="registerModal" @success="reload" />
</template>

<script lang="ts" setup>
import { computed, inject, ref, unref, watch } from 'vue';
import { ActionItem, BasicTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useDrawer } from '/@/components/Drawer';
import { useListPage } from '/@/hooks/system/useListPage';
import UserRecycleBinModal from '../../user/UserRecycleBinModal.vue';
import UserDrawer from '/@/views/system/user/UserDrawer.vue';
import UserSelectModal from '/@/components/Form/src/jeecg/components/modal/UserSelectModal.vue';
import DepartRoleUserAuthDrawer from './DepartRoleUserAuthDrawer.vue';
import { departUserList, linkDepartUserBatch, unlinkDepartUserBatch, deleteUser } from '../depart.user.api';
import { userInfoColumns, userInfoSearchFormSchema } from '../depart.user.data';
import { ColEx } from '/@/components/Form/src/types';
import { useRouter } from 'vue-router';
import PasswordModal from '../../user/PasswordModal.vue';
 //密码model
 const [registerPasswordModal, { openModal: openPasswordModal }] = useModal();
import { string } from 'vue-types';
//回收站model
const [registerModal, { openModal }] = useModal();
const prefixCls = inject('prefixCls');
const props = <any>defineProps({
  data: { require: true, type: Object },
  showEdit: { type: Boolean, default: true },
  selecteddeparts: { type: Object },
  depname: { type: String },
  treeData: { type: Object },
  Companydata: {
    type: Object,
    default: () => ({ remainingMember: "", memo: "", internalInstitutions: "", member: "", realMember: "", remainingChiefPosition: "", remainingDeputyPosition: "", subsidiary: "", chiefPosition: "" })
  },
});
// 当前选中的部门ID，可能会为空，代表未选择部门
const departId = computed(() => props.data?.id);
// 自适应列配置
const adaptiveColProps: Partial<ColEx> = {
  xs: 24, // <576px
  sm: 24, // ≥576px
  md: 24, // ≥768px
  lg: 12, // ≥992px
  xl: 12, // ≥1200px
  xxl: 8, // ≥1600px
};
// 列表页面公共参数、方法
const { tableContext, createMessage } = useListPage({
  tableProps: {
    api: departUserList,
    columns: userInfoColumns,
    canResize: false,
    formConfig: {
      schemas: userInfoSearchFormSchema,
      baseColProps: adaptiveColProps,
      labelAlign: 'left',
      labelCol: {
        xs: 24,
        sm: 24,
        md: 24,
        lg: 9,
        xl: 7,
        xxl: 5,
      },
      wrapperCol: {},
      // 操作按钮配置
      actionColOptions: {
        ...adaptiveColProps,
        style: { textAlign: 'left' },
      },
    },
    // 【issues/1064】列设置的 cacheKey
    tableSetting: { cacheKey: 'depart_user_userInfo' },
    // 请求之前对参数做处理
    beforeFetch(params) {
      params.depId = departId.value;
    },
  },
});

// 注册 ListTable
const [registerTable, { reload, setProps, setLoading, updateTableDataRecord }, { rowSelection, selectedRowKeys }] = tableContext;

watch(
  () => props.data,
  () => reload(),
);
//注册drawer setDrawerProps
const [registerDrawer, { openDrawer }] = useDrawer();
const [registerUserAuthDrawer, userAuthDrawer] = useDrawer();
// 注册用户选择 modal
const [registerSelUserModal, selUserModal] = useModal();

// 清空选择的行
function clearSelection() {
  selectedRowKeys.value = [];
}

// 查看部门角色
function showDepartRole(record) {
  userAuthDrawer.openDrawer(true, { userId: record.id, departId });
}

// 创建用户
function createUser() {
  if (!departId.value) {
    createMessage.warning('请先选择一个部门');
  } else {
    openDrawer(true, {
      isUpdate: false,
      departDisabled: true,
      // 初始化负责部门
      nextDepartOptions: { value: props.data?.key, label: props.data?.title },

      record: {
        selectedDepart: props.selecteddeparts.depId,
        activitiSync: 1,
        userIdentity: 1,
        selecteddeparts: departId.value,
      },
    });
  }
}
function translateNumber(num) {
  const map = { 1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 7: '七', 8: '八', 9: '九', 0: '零' };
  return map[num] || num; // 如果 num 存在于 map 中，则返回对应的汉字，否则返回原值
}
// 查看用户详情
function showUserDetail(record) {
  openDrawer(true, {
    record,
    isUpdate: true,
    departDisabled: true,
    showFooter: false,
  });
}

// 编辑用户信息
function editUserInfo(record) {
  //像编辑体里面塞入公司id props.selecteddeparts.depId
  console.log('像编辑体里面塞入公司id', record)
  record.selectedDepart = props.selecteddeparts.depId
  openDrawer(true, { isUpdate: true, record, departDisabled: true });
}

// 选择添加已有用户
function selectAddUser() {
  selUserModal.openModal();
}

// 批量取消关联部门和用户之间的关系
async function unlinkDepartUser(idList, confirm) {
  if (!departId.value) {
    createMessage.warning('请先选择一个部门');
  } else {
    setLoading(true);
    let userIds = unref(idList).join(',');
    try {
      await unlinkDepartUserBatch({ depId: departId.value, userIds }, confirm);
      return reload();
    } finally {
      setLoading(false);
    }
  }
  return Promise.reject();
}

// 批量取消关联事件
async function onUnlinkDepartUserBatch() {
  try {
    await unlinkDepartUser(selectedRowKeys, true);
    // 批量删除成功后清空选择
    clearSelection();
  } catch (e) { }
}

// 选择用户成功
async function onSelectUserOk(options, userIdList) {
  if (userIdList.length > 0) {
    try {
      setLoading(true);
      await linkDepartUserBatch(departId.value, userIdList);
      reload();
    } finally {
      setLoading(false);
    }
  }
}
const router = useRouter();
/**
 * 用户抽屉表单成功回调
 */
function onUserDrawerSuccess({ isUpdate, values }) {
  isUpdate ? updateTableDataRecord(values.id, values) : reload();
}
//跳转路由
function handleResume(record) {
  const userId = record.id; // 假设 record 中有 userId 字段
  const realname = record.realname; // 假设 record 中有 userId 字段
  console.log(record);
  router.push({
    path: '/excelfile',
    query: { userId, realname }, // 将 userId 作为查询参数传递
  });
}
/**
 * 操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    { label: '编辑', onClick: editUserInfo.bind(null, record), ifShow: props.showEdit }, { label: '履历', onClick: handleResume.bind(null, record) }];
  // if (props.showEdit) {
  //   actions.push({ label: '编辑', onClick: editUserInfo.bind(null, record) });
  // }
  // {
  //     label: '人员履历',
  //     onClick: handleResume.bind(null, record),
  //     // ifShow: () => hasPermission('system:user:edit'),
  //   },
}
  /**
   * 打开修改密码弹窗
   */
   function handleChangePassword(username) {
    openPasswordModal(true, { username });
  }
/**
 * 删除事件
 */
async function handleDelete(record) {
  if ('admin' == record.username) {
    createMessage.warning('管理员账号不允许此操作！');
    return;
  }
  await deleteUser({ id: record.id }, reload);
}
/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '重置密码',
      //auth: 'user:changepwd',
      onClick: handleChangePassword.bind(null, record.username),
    },
    {
      label: '删除',
      ifShow: props.showEdit,
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
    // { label: '部门角色', onClick: showDepartRole.bind(null, record) },
    { label: '用户详情', onClick: showUserDetail.bind(null, record) },
    {
      label: '取消关联',
      color: 'error',
      ifShow: props.showEdit,
      popConfirm: {
        title: '确认取消关联吗？',
        confirm: unlinkDepartUser.bind(null, [record.id], false),
      },
    },
  ];
}
</script>
